package com.gkj.blog.business.service.impl;

import com.gkj.blog.business.service.RoleResourcesService;
import com.gkj.blog.business.vo.entity.IpLogVo;
import com.gkj.blog.business.vo.entity.RoleResources;
import com.gkj.blog.framework.object.AbstractServiceImpl;
import com.gkj.blog.persistence.mapper.SysRoleResourcesMapper;
import com.gkj.blog.persistence.po.SysRoleResources;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.Date;

@Service
public class RoleResourcesServiceImpl extends AbstractServiceImpl<RoleResources,SysRoleResources> implements RoleResourcesService {

    SysRoleResourcesMapper sysRoleResourcesMapper;

    @Autowired
    public void setSysRoleResourcesMapper(SysRoleResourcesMapper sysRoleResourcesMapper) {
        this.sysRoleResourcesMapper = sysRoleResourcesMapper;
        setBaseMapper(sysRoleResourcesMapper);
    }

    @Override
    public void removeByRoleId(Long id) {
        sysRoleResourcesMapper.removeByRoleId(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addRoleResources(Long roleId, Long[] resourcesIds) {
        if(resourcesIds==null||resourcesIds.length==0)
            return false;
        removeByRoleId(roleId);
        for(Long resourcesId:resourcesIds){
            RoleResources roleResources = new RoleResources();
            roleResources.setRoleId(roleId);
            roleResources.setResourcesId(resourcesId);
            insert(roleResources);
        }
        return true;
    }
}
